
layui.use(['laydate','table','form'],
    function() {
        let table = layui.table;
        createSelect()

        createTable()

///执行一个laydate实例
        /* laydate.render({
             elem: '#start' //指定元素
         });*/

//执行一个laydate实例
        /*   laydate.render({
               elem: '#end' //指定元素
           });*/
// crateTable();
        var form = layui.form;
        form.on('submit(sreach)',function (){
            createTable();
            return false;

        });
//监听工具条
        table.on('tool(lawyers)', function(obj) {
            let data = obj.data;
            if (obj.event === 'detail') {
                sessionStorage.setItem("lawyerId", data.id)
                xadmin.open('查看', "/html/lawyer/lawyer-info.html", 600, 400)
            } else if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                let result =myAjax('http://localhost/ssm/back/lawyer/delete',{id: data.id},'post')
                    if(result.code=="200"){
                        $(obj).parents("tr").remove();
                        layer.msg('已删除!', {icon: 1, time: 1000});
                    }
                    obj.del();
                    layer.close(index);
                    return false;

                    //sessionStorage.setItem("lawyerId", data.id)
                });
            } else if (obj.event === 'edit') {
                sessionStorage.setItem("lawyerId", data.id)
                xadmin.open('编辑', "/html/lawyer/lawyer-edit.html", 600, 400)
                //layer.alert('编辑行：<br>' + JSON.stringify(data))
            }

            else if(obj.event === 'home'){
                if(data.isHome=='是'){
                    layer.confirm('是否取消主页显示',function (index) {
                        let result =myAjax('http://localhost/ssm/back/lawyer/delete',{id: data.id},'post')
                        if(result.code=="200"){
                            layer.msg('已改变!', {icon: 1, time: 1000});
                            xadmin.close();
                            xadmin.father_reload();

                        }
                        layer.close(index);
                        return false;
                    })
                }
                else{
                    layer.confirm('是否主页显示',function (index) {
                        let result =myAjax('http://localhost/ssm/back/lawyer/delete',{id: data.id},'post')
                        if(result.code=="200"){
                            layer.msg('已改变!', {icon: 1, time: 1000});
                            xadmin.close();
                            xadmin.father_reload();

                        }
                        layer.close(index);
                        return false;
                    })
                }
            }
        })
    });

function createTable() {

    layui.use(['layer','table'],function () {
        let layer = layui.layer;
        let table = layui.table;
        table.render(
            {
                elem:"#lawyer-list",
                url:"http://localhost/ssm/back/lawyer/list",
                contentType: "application/x-www-form-urlencoded",
                where:{
                    loginName:sessionStorage.getItem("loginName"),
                    lawyer: $("#lawyer").val(),
                    role:$("#roles").val(),
                    isHomeValue:$("#home").val()
                },
                page:true,
                parseData: function (res) {
                    if(res.result=="notLogin"){
                        window.open("/html/login.html", "_top");
                    }
                    console.log(res)
                    return{
                        "code":0,
                        "msg":"",
                        "total":res.data.list.length,
                        "data":res.data.list,
                        "count":res.data.count,
                    };
                },
                cols:[[
                    {field:'id',title:'ID',width:80,sort:true,fixed:'left'},
                    {field:'lawyerName',title:'姓名',width:80},
                    {field:'lawyerIntro',title:'简介',width:250},
                    {field:'head',title:'头像',width:150,templet:'#head'},
                    {field:'role',title:'角色',width:150},
                    {field:'isHomeValue',title:'是否主页显示 ',width:100},
                    {fixed: 'right', width:250, align:'center', toolbar: '#barDemo'}
                ]],

            }
        );






    })


    console.log(2)
}

function createSelect() {
    let select = $("#roles")


    layui.use('form',function () {

        let result=myAjax('http://localhost/ssm/back/lawyer/role',{},'get')
        select.empty()
        select.append(new Option("全部","全部"))
        let data =result.data;
        $.each(data,function (index, item) {

            select.append(new Option(item.role,item.role))
            console.log(select)
        })
        layui.form.render("select");

    })
}



